package com.gome.axwl.aoc.facade.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.anxl.sso.client.api.Account;
import com.anxl.sso.client.api.AccountInfo;
import com.gome.axwl.aoc.common.enumCode.DefaultTypeCode;
import com.gome.axwl.aoc.common.util.BeanJsonUtil;
import com.gome.axwl.aoc.entity.dto.AmAuditSettingDto;
import com.gome.axwl.aoc.entity.po.AmAuditSettingPo;
import com.gome.axwl.aoc.facade.AuditSettingFacade;
import com.gome.axwl.aoc.service.AuditSettingService;
import com.gome.commons.core.ErrorCode;
import com.gome.commons.core.ResultData;

/**
 * @Description: 登录设备管理
 * @Auther: chengh
 * @Date: 2018/4/25 13:59
 */

@Service
public class AuditSettingFacedeImpl implements AuditSettingFacade {


    private final static Logger log = LoggerFactory.getLogger(AuditSettingFacedeImpl.class);

    @Autowired
    private AuditSettingService auditSettingService;

    /**
     * 新增
     *
     * @param amAuditSettingDto
     * @return
     */
    @Override
    @Transactional
    public ResultData addAuditSetting(AmAuditSettingDto amAuditSettingDto) {
        try {
            AmAuditSettingPo amAuditSettingPo = new AmAuditSettingPo();
            amAuditSettingPo.setDefaultType(amAuditSettingDto.getDefaultType());
            // 根据defaultType去查是否当前默认权限是否已设置
            AmAuditSettingPo amAuditSettingPo1 = BeanJsonUtil.beanConvert(amAuditSettingDto, AmAuditSettingPo.class);
            // 新增
            amAuditSettingPo1.setDefaultId(amAuditSettingDto.getDefaultType());
            amAuditSettingPo1.setCreateTime(new Date());
            amAuditSettingPo1.setUpdateTime(new Date());
            auditSettingService.addAuditSetting(amAuditSettingPo1);
        } catch (DuplicateKeyException e) {
            e.printStackTrace();
            return new ResultData(com.gome.axwl.aoc.common.util.ErrorCode.DUPLICATE_ERROR, com.gome.axwl.aoc.common.util.ErrorCode.DUPLICATE_ERROR_MSG);
        } catch (Exception e) {
            log.error("新增默认审核配置失败，设备信息：{}，异常信息：{}", amAuditSettingDto.toString(), e.toString());
            return new ResultData(ErrorCode.FAILOR, ErrorCode.FAILOR_MSG);
        }
        return new ResultData(ErrorCode.SUCCESS, ErrorCode.SUCCESS_MSG);
    }

    @Override
    public ResultData deleteAuditSetting(int defaultId) {
        return auditSettingService.deleteAuditSettingById(defaultId);
    }

    @Override
    public ResultData updateAuditSetting(AmAuditSettingDto amAuditSettingDto) {
        AmAuditSettingPo amAuditSettingPo = BeanJsonUtil.beanConvert(amAuditSettingDto, AmAuditSettingPo.class);
        Integer updateCode = auditSettingService.updateAuditSettingByDefaultId(amAuditSettingPo);
        return ResultData.newResultData(updateCode);
    }

    @Override
    public ResultData getAuditSettingList(AmAuditSettingDto amAuditSettingDto) {
        AmAuditSettingPo amAuditSettingPo = BeanJsonUtil.beanConvert(amAuditSettingDto, AmAuditSettingPo.class);
        List<AmAuditSettingPo> list = auditSettingService.selectSelective(amAuditSettingPo);
        
        List<AmAuditSettingDto> returnList = new ArrayList<AmAuditSettingDto>();

        //组装数据列表
        list.forEach((poItem) -> {
            //复制基本属性
            AmAuditSettingDto returnItem = BeanJsonUtil.beanConvert(poItem, AmAuditSettingDto.class);
            //获取accountNo
            String accountNo = poItem.getAuditUser();
            AccountInfo accountInfo = auditSettingService.getUserInfoByAccountNo(accountNo);
            if (accountInfo != null) {
                //姓名+账号
                returnItem.setAuditUserName(accountInfo.getName());
                returnItem.setAccount(accountInfo.getAccount());
                returnItem.setInvitationCode(accountInfo.getInvitationCode());
                //在职状态
                returnItem.setIsJobInService(String.valueOf(accountInfo.getJobStatus()));
                //角色
                returnItem.setDefaultTypeName(DefaultTypeCode.getDefaultTypeByCode(poItem.getDefaultType()).getName());
            }
            returnList.add(returnItem);
        });
        return ResultData.newResultData(returnList);
    }

    @Override
    public Boolean isUserHasAuditPrivilege(String accountNo) {
        return auditSettingService.isUserHasAuditPrivilege(accountNo);
    }

    @Override
    public ResultData filterAuditUserList(String keyword) {
        ResultData filterResultData = auditSettingService.getAuditPrivilegeList(keyword);
        if (ResultData.isSuccess(filterResultData)) {
            //三方调用返回成功
            Map map = new BeanMap(filterResultData.getData());
            //初始化返回数据集
            Map<String, Object> returnMap = new HashMap<>();
            //获取返回数据列表
            List<Map> dataList = (List<Map>) map.get("list");

            if (null == dataList || dataList.isEmpty()) {
                //正常返回数据
                return ResultData.newResultData(returnMap);
            }
            for (Map itemMap : dataList) {
                String accountNo = itemMap.get("accountNo") + "";
                String account = itemMap.get("account") + "";
                String name = itemMap.get("name") + "";
                String invitationCode = itemMap.get("invitationCode") + "";
                returnMap.put("accountNo", accountNo);
                returnMap.put("account", account);
                returnMap.put("name", name);
                returnMap.put("invitationCode", invitationCode);
            }
            //正常返回数据
            return ResultData.newResultData(returnMap);
        } else {
            //调用三方返回 三方原始失败
            return filterResultData;
        }
    }

    @Override
    public List<Account> queryAccountByKeyword(String keyWord) {
        return auditSettingService.queryAccountByKeyword(keyWord);
    }


}
